package com.jump.user.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jump.user.dao.UserRepository;
import com.jump.user.dto.User;
import com.jump.user.util.Md5Util;
import com.jump.user.util.ValidateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by fangxiao on 15/9/1.
 */
@Service
public class UserService {



    @Autowired
    private UserRepository repository;

    /**
     * 检查用户名是否诶使用
     * @param userName
     * @return
     */
    public Map exitUserByUserName(String userName) {
        Map map = new HashMap();
        String resCode = "000000";
        String message = "此用户名可以使用";
        User user = repository.findByUserName(userName);
        if(user!=null) {
            resCode = "000001";
            message = "此用户名已经被使用，请您使用其它用户名";
        }
        map.put("resCode",resCode);
        map.put("message",message);
        return map;
    }

    /**
     * 检查手机是否被使用
     * @param mobile
     * @return
     */
    public Map exitUserByMobile(String mobile) {
        Map map = new HashMap();
        String resCode = "000000";
        String message = "此手机号码可以使用";
        User user = repository.findByMobile(mobile);
        if(user!=null) {
            resCode = "000001";
            message = "此手机号码已经被使用，请您使用其它手机号码";
        }
        map.put("resCode",resCode);
        map.put("message",message);
        return map;
    }

    /**
     * 检查邮箱是否被使用
     * @param email
     * @return
     */
    public Map exitUserByEmail(String email) {
        Map map = new HashMap();
        String resCode = "000000";
        String message = "此email可以使用";
        User user = repository.findByEmail(email);
        if (user != null) {
            resCode = "000001";
            message = "此邮箱已经被使用，请您使用其它邮箱";
        }
        map.put("resCode",resCode);
        map.put("message",message);
        return map;
    }


    /**
     *
     * @param userName
     * @param password
     * @param mobile
     * @param email
     * @return
     */
    public Map register(String userName, String password, String mobile, String email) {
        Map map = new HashMap();
        Map validateMap = new HashMap();
        String resCode = "000000";
        String message = "注册成功";
        if(ValidateUtil.isEmpty(userName)) {
            validateMap.put("user-require","用户名不能为空");
        }
        if(ValidateUtil.isEmpty(password)) {
            validateMap.put("password-require","密码不能为空");
        }
        if(ValidateUtil.isEmpty(mobile)) {
            validateMap.put("mobile-require","手机不能为空");
        }
        if(!ValidateUtil.isEmail(email)) {
            validateMap.put("email-format","email格式不正确");
        }
        if(!ValidateUtil.isMobile(mobile)) {
            validateMap.put("mobile-format","mobile格式不正确");
        }
        if(!validateMap.isEmpty()) {
            resCode = "000001";
            message = "注册失败,请检查非空或者格式";
        }
        else if(repository.findByUserName(userName)!= null) {
            resCode = "000002";
            message = "注册失败,用户名已存在";
        }
        else if(repository.findByMobile(mobile)!= null) {
            resCode = "000003";
            message = "注册失败,手机号码已被使用";
        }
        else if(repository.findByEmail(email)!=null) {
            resCode = "000004";
            message = "注册失败,邮箱已被使用";
        }
        else {
            User user = new User();
            user.setUserName(userName);
            user.setMobile(mobile);
            user.setEmail(email);
            try {
                password = Md5Util.getEncryptedPwd(password);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            user.setPassword(password);

            repository.save(user);
            map.put("userMap", this.getUserMap(user));
        }

        map.put("resCode",resCode);
        map.put("message",message);
        map.put("validateMap", validateMap);
        return map;

    }

    /**
     * 登陆
     * @param info
     * @param password
     * @return
     */
    public Map login(String info, String password) {
        Map map = new HashMap();
        Map validateMap = new HashMap();
        String resCode = "000000";
        String message = "登录成功";
        User user = null;
        user = repository.findByUserName(info);
        if(ValidateUtil.isEmpty(info)) {
            validateMap.put("info-require","用户名或手机或email不能为空");
        }
        if(ValidateUtil.isEmpty(password)) {
            validateMap.put("password-require","密码不能为空");
        }
        if(!validateMap.isEmpty()) {
            resCode = "000001";
            message = "登陆失败，请检查非空或格式";
        }
        else {

            if (user == null) {
                user = repository.findByMobile(info);
                if (user == null) {
                    user = repository.findByEmail(info);
                }
            }

            if (user == null) {
                resCode = "000002";
                message = "登陆失败，用户名或密码错误";
            } else {
                try {
                    if (!Md5Util.validPassword(password, user.getPassword())) {
                        resCode = "000003";
                        message = "登陆失败，用户名或密码错误";
                    }
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
        map.put("userMap", this.getUserMap(user));
        map.put("resCode",resCode);
        map.put("message",message);
        return map;
    }

    /**
     * 密码重置
     * @param userName
     * @param oldPassword
     * @param newPassword
     * @return
     */
    public Map resetPassword(String userName, String oldPassword, String newPassword) {
        Map map = new HashMap();
        String resCode = "000000";
        String message = "密码重置成功";
        User user = repository.findByUserName(userName);
        try {
            if(!Md5Util.validPassword(oldPassword, user.getPassword())) {
                resCode = "000001";
                message = "旧密码不正确，请重新输入";
            }
            else {
                try {
                    user.setPassword(Md5Util.getEncryptedPwd(newPassword));
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                repository.save(user);
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        map.put("user", user);
        map.put("resCode",resCode);
        map.put("message",message);
        return map;

    }

    /**
     * 实名认证
     * @param userName
     * @param chineseName
     * @param idCard
     * @return
     */
    public Map Certification(String userName,String chineseName, String idCard) {
        Map map = new HashMap();
        Map validateMap = new HashMap();
        String resCode = "000000";
        String message = "实名认证成功";
        if(ValidateUtil.isEmpty(chineseName)) {
            validateMap.put("chinesename-require","姓名不能为空");
        }
        if(ValidateUtil.isEmpty(idCard)) {
            validateMap.put("idcard-require","身份证不能为空");
        }
        if(!validateMap.isEmpty()) {
            resCode = "000001";
            message = "实名认证失败，请检查非空或格式";
        }
        else {
            User user = repository.findByUserName(userName);
            user.setChineseName(chineseName);
            user.setIdCard(idCard);
            repository.save(user);
            map.put("userMap", this.getUserMap(user));
        }
        map.put("resCode", resCode);
        map.put("message",message);
        return map;
    }

    /**
     * 完善用户其它信息
     * @param user
     * @return
     * @throws CloneNotSupportedException
     */
    public Map finishOtherInfo(User user)  {
        Map map = new HashMap();
        String resCode = "000000";
        String message = "完善信息成功";
        repository.save(user);
        map.put("userMap", this.getUserMap(user));
        map.put("resCode", resCode);
        map.put("message",message);
        return map;

    }


    private Map getUserMap(User user) {
        Map map = new HashMap();
        map.put("userName", user.getUserName());
        map.put("mobile", user.getMobile());
        map.put("email",user.getEmail());

        return map;

    }

//    private boolean isCorrectPassword(String password, String pwd) {
//        pwd  = Md5Util.convertMD5(Md5Util.convertMD5(pwd));
//        return password.equals(pwd);
//    }
}
